// 第三方库
import Vue from "vue";
import $ from "jquery";

// 原生插件
import {os,StateBarStyle, ShareTargets} from "../hos/os.js";
import {wx} from "../hos/wx.js";
import {http} from "../hos/http.js";
import {pick} from "../hos/pick.js";

// 页面
import {Activity} from "./activity.js";
import {BleHelper} from "./blehelper.js";
import {AirKiss} from "./airkiss.js";
import {BdAsrTest} from "./bdAdrTest.js";
import {UdpTest} from "./udptest.js";
import {qrcode} from "../hos/qrcode.js";
import {NotificationTest} from "./notificationtest.js";
import {AudioRecorder} from "./audioRecorder";
import {Home2} from "./home2";
import {Native} from "../hos/native";

class Home extends Activity{
    onCreate() {
        super.onCreate();

        new Vue({
            el: this.find("2checker"),
            methods: {
                click: () => {
                    this.jump(new Home2("home2"), {});
                }
            }});

        new Vue({
            el: this.find("wxcode"),
            methods: {
                click: () => {
                    this.jump(new AudioRecorder("audioRecorder"), {});
                }
            }});

        new Vue({
            el: this.find("tcptest"),
            methods: {
                click: () => {
                    this.jump(new BdAsrTest("bdAsrTest"), {});
                }
            }});

        this.isKeepScreenOn = false;

        new Vue({
            el: this.find("postjson"),
            methods: {
                click: () => {
                    if (this.isKeepScreenOn) {
                        os.enableKeepScreenOn(false);
                        this.isKeepScreenOn = false;
                    } else {
                        os.enableKeepScreenOn(true);
                        this.isKeepScreenOn = true;
                    }
                }
            }});

        new Vue({
            el: this.find("posturl"),
            methods: {
                click: () => {
                    const fun = function (event){
                        alert("test");
                    };
                    Native.getInstance().addObserver("test", this, fun);
                    Native.getInstance().addObserver("test", this, fun);
                    Native.getInstance().delObserver("test", fun);
                    Native.getInstance().__fireEvent("test", 0, {});
                }
            }});


        new Vue({
            el: this.find("2checkerblehelper"),
            methods: {
                click: () => {
                    this.jump(new BleHelper("blehelper"), {});
                }
            }});

        function testObserver(event) {
            alert(JSON.stringify(event));
        }

        new Vue({
            el: this.find("udptest"),
            methods: {
                click: () => {
                    this.jump(new UdpTest("udptest"), {});
                }
            }});

        new Vue({
            el: this.find("qrcode"),
            methods: {
                click: () => {
                    qrcode.scan().then((result)=>{alert(result.code);}).catch((error)=>{alert(error.errMsg);});
                }
            }});

        new Vue({
            el: this.find("upgrade"),
            methods: {
                click: () => {
                    if (os.openTestMode()) {
                        alert("前面有老虎，非测试人员如果无意进入请马上重启APP");
                    }
                }
            }});

        new Vue({
            el: this.find("airkiss"),
            methods: {
                click: () => {
                    this.jump(new AirKiss("airkiss"), {});
                }
            }});

        new Vue({
            el: this.find("notification"),
            methods: {
                click: () => {
                    alert("通知");
                    this.jump(new NotificationTest("notificationtest"), {});
                }
            }});

        new Vue({
            el: this.find("pickimages"),
            methods: {
                click: () => {
                    os.enableHardware(false);
                    pick.screenshot()
                    // pick.images({count:9})
                        .then((resp)=>{
                            os.enableHardware(true);
                            os.openSetting();
                            // images有若干图片，我这里只是显示第一张
                            $(this.find("pickimagepreview")).attr("src",resp.images[0].url);
                            // 返回的是一个task，因此你可以打断
                            // os.shareImage(resp.images[0].url);
                            // os.shareText("测试：我分享了一条文本")
                            // os.shareWebPage("https://codbo.cn/blog-22.html", "标题", "描述",
                            //    "https://codbo.cn/zb_users/upload/2017/09/201709191505820453309591.png")
                            http.getByXhr(resp.images[0].url).then((xhr)=>{
                                // xhr.response
                                alert("读取文件成功->" + xhr.response.type);
                                let formData = new FormData();
                                // .jpg是乱写的 可能还有其他类型 根据xhr.response.type 就可以推出
                                let fileName = xhr.response.type.replace("/", ".");
                                alert(fileName);
                                formData.append("pic", xhr.response, fileName);
                                // formData.append('file', xhr.response, "test.jpg");
                                // $.ajax({
                                //     url:'https://h5.henortek.cn/Wxlogin/uploadPic',
                                //     // url: 'https://h5.henortek.cn/test/upload/index.php',
                                //     type: 'POST',
                                //     cache: false,
                                //     data: formData,
                                //     processData: false,
                                //     contentType: false,
                                //     global:false,
                                // }).done(function(res) {
                                //     console.log("发送图片结果:" , res)
                                //     alert('发送图片结果：' + res);
                                // }).fail(function(xhr,textStatus, errorThrown) {
                                //     alert('发送图片失败：' + xhr.statusText);
                                // });
                            }, (error)=>{
                                alert("读取文件错误");
                            });
                        });
                }
            }});
    }

    onResume() {
        super.onResume();
        os.setStateBarStyle(StateBarStyle.DEFAULT);
    }
}

export {Home};
